﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DataControl;
using System.Data;
using System.Text;

namespace ATW.MT.BUS
{
    public class bus_D_Item
    {
        DataAccess da = null;

        public bus_D_Item()
        {
            da = new DataAccess();
        }

        #region 方法
        /// <summary>
        /// 新增子项
        /// </summary>
        /// <param name="account"></param>
        /// <param name="pwd"></param>
        /// <param name="level_class"></param>
        /// <returns></returns>
        public int AddItem(string strName, string strDesc, string strUnit)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"insert into D_Item(diName,diDesc,diUnit,diRowStatus) values('{0}','{1}','{2}','1')",
                strName, strDesc, strUnit);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 新增子项
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="strDesc"></param>
        /// <param name="strUnit"></param>
        /// <param name="catid">所属类别id</param>
        /// <returns></returns>
        public int AddItem(string strName, string strDesc, string strUnit, string catid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"insert into D_Item(diName,diDesc,diUnit,diRowStatus,ticid) values('{0}','{1}','{2}','1','{3}')",
                strName, strDesc, strUnit, catid);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 新增子项
        /// </summary>
        /// <param name="name"></param>
        /// <param name="desc"></param>
        /// <param name="unit"></param>
        /// <param name="catid"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        public int AddItem(string name, string desc, string unit, string catid, string level)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"insert into D_Item(diName,diDesc,diUnit,diRowStatus,ticid,[level])
                            values('{0}','{1}','{2}','0','{3}','{4}')", name, desc, unit, catid, level);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 查询类别
        /// </summary>
        /// <returns></returns>
        public DataTable CateQuery()
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select ticid,ticname from dbo.T_InformationCategory where ticRowStatus <>'2'");
            return da.ExecuteDataTable(sql.ToString());
        }
        /// <summary>
        /// 修改子项
        /// </summary>
        /// <param name="account"></param>
        /// <param name="pwd"></param>
        /// <param name="level_class"></param>
        /// <returns></returns>
        public int ModifyItem(string strDiid, string strName, string strDesc, string strUnit)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"update D_Item set diName='{0}',diDesc='{1}',diUnit='{2}',UpdatedDate=getdate() where diid={3}",
                strName, strDesc, strUnit, strDiid);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 修改子项
        /// </summary>
        /// <param name="strDiid"></param>
        /// <param name="strName"></param>
        /// <param name="strDesc"></param>
        /// <param name="strUnit"></param>
        /// <param name="strcid">所属类别id</param>
        /// <returns></returns>
        public int ModifyItem(string strDiid, string strName, string strDesc, string strUnit, string strcid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"update d_item set diName='{0}',diDesc='{1}',diUnit='{2}',ticid='{3}',UpdatedDate=getdate() where diid='{4}'",
                strName, strDesc, strUnit, strcid, strDiid);
            return da.ExcuteNoQuery(sql.ToString());
        }
        public int ModifyItem(string diid, string name, string desc, string unit, string catid, string level)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"update d_item set diName='{0}',diDesc='{1}',diUnit='{2}',ticid='{3}',UpdatedDate=getdate(),[level]='{4}' where diid='{5}'",
                name, desc, unit, catid, level, diid);
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 用户分页查询
        /// </summary>
        /// <param name="account">账号</param>
        /// <param name="status">状态，1正常，2删除</param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public DataTable UserPagerQuery(string strDiName, int page, int pagesize)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select DIID,diName,diDesc,diUnit,diRowStatus,CreatedDate from D_Item where diRowStatus=0 and  1=1");
            if (strDiName != "")
            {
                sql.Append(" and diName like '%" + strDiName + "%'");
            }

            return da.PageSize(sql.ToString(), page, pagesize, "DIID");
        }
        /// <summary>
        /// 用户分页查询
        /// </summary>
        /// <param name="strDiName"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public DataTable UserPagerQueryEx(string strDiName, int page, int pagesize)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select diid,diname,didesc,diunit,dirowstatus,i.CreatedDate,c.ticName
	                        from D_Item i left join dbo.T_InformationCategory c on i.ticid=c.TICID where 1=1 and i.dirowstatus<>'2'");
            if (strDiName != "")
            {
                sql.Append(" and diName like '%" + strDiName + "%'");
            }
            return da.PageSize(sql.ToString(), page, pagesize, "DIID", "CreatedDate desc");
        }
        /// <summary>
        /// 子项分页查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public DataTable ItemPagerQuery(string name, string status, int page, int pagesize)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select i.DIID,
	                                   i.diName,
	                                   i.diDesc,
	                                   info.TICID,
	                                   info.ticName,
	                                   i.diUnit,
	                                   i.[Level],
                                       i.dirowstatus,
	                                   i.CreatedDate from D_Item i 
                                left join T_InformationCategory info on i.TICID=info.TICID where 1=1");
            if (!string.IsNullOrEmpty(name))
            {
                sql.Append(" and i.diname like '%" + name + "%'");
            }
            if (!string.IsNullOrEmpty(status))
            {
                sql.Append(" and i.diRowStatus ='" + status + "'");
            }
            return da.PageSize(sql.ToString(), page, pagesize, "DIID", "CreatedDate desc");
        }
        /// <summary>
        /// 改变用户状态
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="status">1正常 2封禁</param>
        /// <returns></returns>
        public int UserChangeStatus(string strId, string strStatus)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"update D_Item set diRowStatus='" + strStatus + "' where DIID='" + strId + "'");
            return da.ExcuteNoQuery(sql.ToString());
        }
        /// <summary>
        /// 类别是否指定了供应商
        /// </summary>
        /// <param name="itemid"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        public bool canLevelBeSet(string catid, string level)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@"select isnull(info.level_c_supply,'') c,
	                           isnull(info.level_d_supply,'') d from T_InformationCategory info where ticid='" + catid + "'");
            DataTable dt = da.ExecuteDataTable(sql.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                string level_C = dt.Rows[0]["c"].ToString();
                string level_D = dt.Rows[0]["d"].ToString();
                if (level == "C" && level_C != "")
                {
                    return true;
                }
                if (level == "D" && level_D != "")
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 子项是否能够删除
        /// </summary>
        /// <param name="itemid"></param>
        /// <returns></returns>
        public bool canItemBeDel(string itemid)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select * from T_DemandInformationDetail where diid='{0}'", itemid);
            DataTable dt = da.ExecuteDataTable(sql.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                return false;
            }
            return true;
        }
        public DataTable SelectItem(string strId)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select * from  D_Item where DIID='" + strId + "'");
            return da.ExecuteDataTable(sql.ToString());
        }
        #endregion
    }
}